#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
// #include "Timer.h"
// #include "MatrixKey.h"
#include "W25Q128.h"

unsigned char xdata test_data[] = "Hello, W25Q128";
unsigned char xdata read_buffer[200];

void main()
{
	unsigned int id;
	unsigned int i;
	// unsigned char KeyNumber;
	// Timer0_Init();
	LCD1602_Init();
	W25Q128_Init();
	
	LCD1602_PrintString(1,1,"W25Q128 TEST");
	Delay(1500);
	LCD1602_PrintString(1,1,"                ");

	id=W25Q128_ReadID();
	LCD1602_PrintString(1,1,"ID:");
	LCD1602_PrintHexNum(1,4,id,4);
	Delay(2000);
	LCD1602_PrintString(1, 1, "                ");

	// 擦除第一个扇区
	LCD1602_PrintString(1, 1, "Erasing...");
	W25Q128_EraseSector(0);
	W25Q128_VerifyErase(0);
	LCD1602_PrintString(1, 1, "                ");
	LCD1602_PrintString(1, 1, "Erase Done");
	Delay(1000);
	LCD1602_PrintString(1, 1, "                ");

	// 写入测试数据
	LCD1602_PrintString(1, 1, "Writing...");
	W25Q128_EnhancedWriteData(0x000000, test_data, sizeof(test_data));
	LCD1602_PrintString(1, 1, "                ");
	LCD1602_PrintString(1, 1, "Write Done");
	Delay(1000);
	LCD1602_PrintString(1, 1, "                ");

	// 读取数据并验证
	LCD1602_PrintString(1, 1, "Reading...");
	W25Q128_ReadData(0x000000, read_buffer, sizeof(test_data));
	LCD1602_PrintString(1, 1, "                ");

	// 验证数据
	for (i = 0; i < sizeof(test_data); i++)
	{
		if (test_data[i] != read_buffer[i])
		{
			LCD1602_PrintString(1, 1, "Verify Failed!");
			while (1)
				;
		}
	}

	// 显示读取的数据
	LCD1602_PrintString(1, 1, "Verify OK!");
	Delay(1000);
	LCD1602_PrintString(2, 1, read_buffer);

	while(1)
	{

	}
}
